package lvxu.controller;

import com.github.pagehelper.PageInfo;
import lvxu.bean.Msg;
import lvxu.bean.User;
import lvxu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//@SessionAttributes("user")
@Controller
public class UserController {
    @Autowired
    UserService userService;

    //修改某一用户信息
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateUser(User user){
        System.out.println("你好==="+user+"不好");
        User new_user = userService.updateUser(user);
        System.out.println(new_user);
        return Msg.success();
    }

    //添加用户信息
    @RequestMapping(value = "/useradd",method = RequestMethod.POST)
    @ResponseBody
    public Msg addUser(User user){
        System.out.println("你好==="+user+"不好");
        boolean b = userService.saveUser(user);
        return b?Msg.success():Msg.fail();
    }
    //查询所有用户信息
    @RequestMapping(value = "/userall",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectUsers(){
        List<User> users = userService.selectUsers();
        return Msg.success().add("users",users);
    }
    //删除用户信息
    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg selectUsers(@PathVariable("id") Integer id){
        boolean delete = userService.deleteById(id);
        return delete?Msg.success():Msg.fail();
    }

    //注册用户功能
    @RequestMapping(value = "/usersave",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView registerUser(User user){
        System.out.println("你好==="+user+"不好");
        userService.saveUser(user);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }
//    //修改用户功能  全部
//    @RequestMapping(value = "/user",method = RequestMethod.PUT)
//    @ResponseBody
//    public void updateUser(User user){
//        System.out.println("你好==="+user+"不好");
//        User user1 = userService.updateUser(user);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("music_list");
//        modelAndView.addObject("user",user1);
//        System.out.println("=============______"+ user1);
//        login(user1);
////        return modelAndView;
//    }
    //修改用户功能   状态
    @RequestMapping(value = "/user/{userid}",method = RequestMethod.PUT)
    @ResponseBody
    public void updateState(@PathVariable("userid")Integer userid){
        User user = userService.selectByUserId(userid);
        user.setUserstate(1);
        userService.updateState(user);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("music_list");
        modelAndView.addObject("user",user);
        login(user);
//        return modelAndView;
    }
    //修改用户状态功能
    @RequestMapping(value = "/user/{userid}",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectState(@PathVariable("userid")Integer userid){
        User user = userService.selectByUserId(userid);
        return  Msg.success().add("user",user);
    }

    //用户登录功能
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView login(User user){

        System.out.println(user);
        boolean login = userService.login(user);
        ModelAndView modelAndView = new ModelAndView();

        if (login){
            modelAndView.setViewName("music_list");
            List<User> user1 = userService.selectUser(user.getUsername());
            modelAndView.addObject("user",user1);
        }else {
            modelAndView.setViewName("login");
            Msg message = Msg.fail().add("message", "用户名或密码错误！！！");
            modelAndView.addObject("error",message);
        }
        //导航页码数
//        PageInfo<CollectList> pageInfo = new PageInfo<>(collectlistList);
//        return Msg.success().add("collect",pageInfo);
        return modelAndView;
    }
}
